***************************************************************************************************
* Title: Prepare data for map of number of studies planning to recruit participants in each country
* Manuscript Ref : Figure 3b of the manuscript
* Version: 14.2
* Required packages: none 
***************************************************************************************************

version 14.2

*Change to folder where the dataset is stored
cd "filepath"
use 2020-05-07_iddo_lsr_covid-19_sr.dta, clear

*Change to folder where you want results saved
cd "filepath"


*Only keep one instance of each country/ID combo
keep if pickone_ctry_study_record==1
bysort st_ctry_name: gen within_ctry_num = _n


gen total_studies = 1
label variable total_studies "Number studies"

*Cut continuous variable into a categorised variable
foreach var_to_count in total_studies {
bysort st_ctry_name: egen ctry_total_`var_to_count' = total(`var_to_count')
label variable ctry_total_`var_to_count' "`: var label `var_to_count''"
*Categorise the variable
gen ctry_total_`var_to_count'_str = "1" if ctry_total_`var_to_count'==1
replace ctry_total_`var_to_count'_str = "2 to 3" if ctry_total_`var_to_count'>1 & ctry_total_`var_to_count'<4
replace ctry_total_`var_to_count'_str = "4 to 5" if ctry_total_`var_to_count'>3 & ctry_total_`var_to_count'<6
replace ctry_total_`var_to_count'_str = "6 to 10" if ctry_total_`var_to_count'>5 & ctry_total_`var_to_count'<11
replace ctry_total_`var_to_count'_str = ">10" if ctry_total_`var_to_count'>10 
encode ctry_total_`var_to_count'_str, gen(ctry_total_`var_to_count'_cat)
}


*Tag one record per country
egen pickone_country = tag(st_ctry_name)

*Generate an iso_a3 variable from isoalpha3code to merge
gen iso_a3 = isoalpha3code

*Export data for R 
export delimited ctry_total* iso_a3 using "data_mapping_country_numbers.csv" if pickone_country==1, delimiter(",") replace nolabel
